package com.gable.report.service.impl;


import com.gable.report.dao.IChartsForAreaOverviewDao;
import com.gable.report.model.*;
import com.gable.report.service.ChartsForAreaOverviewService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.gable.report.util.ChartsUtil.getParamMap;

/**
 * @Author: 杨青桦
 * @Description:  区报表运营管理service实现类
 * @Date: 2018/8/14 14:54
 */
@Service
public class ChartsForAreaOverviewServiceImpl implements ChartsForAreaOverviewService {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    IChartsForAreaOverviewDao overviewDao;

    @Override
    public Map<String, Object> queryAreaRegion(Long areaId) {
        return overviewDao.queryAreaRegion(areaId);
    }

    @Override
    public Map<String, Object> queryHospitalLevels(User user, ChartsAreaQuery chartsAreaQuery) throws Exception{
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(user, chartsAreaQuery);
        List<Map<String,Object>> list = overviewDao.queryHospitalLevels(paramMap);
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryAssetsTypeAndRisk(User user, ChartsAreaQuery chartsAreaQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(user, chartsAreaQuery);
        ChartsAreaOverviewBaseView totalAssets = overviewDao.queryAllAssetsConditions(paramMap);
        if(totalAssets == null || totalAssets.getCurCounts() == 0){
            return null;
        }
        List<ChartsAreaOverviewBaseView> list = overviewDao.queryFirstTypeConditions(paramMap);
        //一级68类型计算其他
        /*
        Long totalCounts = 0L; //重点一级68分类总数量
        Long totalPrice = 0L;  //重点一级68分类总金额
        Long totalLastCounts = 0L; //去年重点一级68分类总数量
        if(!CollectionUtils.isEmpty(list)){
            totalCounts = list.stream().map(x->x.getCurCounts()).reduce(0L,Long::sum);
            totalPrice = list.stream().map(x->x.getCurPrice()).reduce(0L,Long::sum);
            totalLastCounts = list.stream().map(x->x.getLastCounts()).reduce(0L,Long::sum);
        }
        ChartsAreaOverviewBaseView obj = new ChartsAreaOverviewBaseView();
        obj.setId(-1L);
        obj.setName("其他");
        obj.setCurCounts(totalAssets.getCurCounts()-totalCounts);
        obj.setCurPrice(totalAssets.getCurPrice()-totalPrice);
        obj.setLastCounts(totalAssets.getLastCounts() - totalLastCounts);
        //其他设备数量同比计算
        if(obj.getCurCounts() == 0 && obj.getLastCounts() == 0){
            obj.setCountsYoY(0.0);
        }else if(obj.getLastCounts() == 0 && obj.getCurCounts() > 0){
            obj.setCountsYoY(100.0);
        }else{
            obj.setCountsYoY((obj.getCurCounts()-obj.getLastCounts())*100.0/obj.getLastCounts());
        }

        list.add(obj);
        */
        List<Map<String,Object>> riskMap = overviewDao.queryRiskLevelConditions(paramMap);
        returnMap.put("total",totalAssets);
        returnMap.put("type",list);
        returnMap.put("risk",riskMap);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryHospitalServicePeopleList(User user, ChartsAreaQuery chartsAreaQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(user, chartsAreaQuery);
        List<Map<String,Object>> list = overviewDao.queryHospitalServicePeopleList(paramMap);
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryServicePeoplePie(User user, ChartsAreaQuery chartsAreaQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(user, chartsAreaQuery);
        List<Map<String,Object>> list = overviewDao.queryHospitalServicePeopleList(paramMap);
        Map<String,Object> totalAssets = overviewDao.queryAllAssetsServicePeople(paramMap);
        //取前五名
        if(!CollectionUtils.isEmpty(list) && list.size() > 5){
            list = list.subList(0,5);
            for(Map<String,Object> map : list){
                map.put("curDayUsedCounts", 0);
            }
        }
        totalAssets.put("curDayUsedCounts",0);
        returnMap.put("total",totalAssets);
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryHospitalMap(User user, ChartsAreaQuery chartsAreaQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(user, chartsAreaQuery);
        List<Map<String,Object>> list = overviewDao.queryHospitalMap(paramMap);
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryHospitalRepair(User user, ChartsAreaQuery chartsAreaQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(user, chartsAreaQuery);
        List<ChartsMaintainInfoView> list = overviewDao.queryHospitalRepairList(paramMap);
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryHospitalUpkeep(User user, ChartsAreaQuery chartsAreaQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(user, chartsAreaQuery);
        List<ChartsForMaintenance> list = overviewDao.queryHospitalUpkeepList(paramMap);
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryHospitalCompulsory(User user, ChartsAreaQuery chartsAreaQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(user, chartsAreaQuery);
        List<ChartsForMaintenance> list = overviewDao.queryHospitalCompulsoryList(paramMap);
        returnMap.put("list",list);
        return returnMap;
    }


}
